#include <stdio.h>
void itoa(int n, char s[]);
void reverse(char s[]);
int clear(char* c, int count);
#define MAGIC_1 2048
#define MIN_INT -2147483648

/** the int range is from -2147483648 to 2147483647 */
/** the highest bit 2147483648 is the bit for 'negative' */
/** it goes there are 2147483648 numbers below 0, 2147483647 above 0 and 0 */
int main()
{
  char s[MAGIC_1];
  int k= -1;
  //  int l = -k;
  //printf("l: %i\n",l);
  //	 itoa(  -2147483648,s);
  // 	 printf("%s\n",s);
  for(;;)
    {
      clear(s,MAGIC_1);
	 itoa(k,s);
 	 printf("%i :\t %s\n",k,s);
	 k*=2;
    }
  
  return 0;
}  
/* itoa:  convert n to characters in s */

int clear(char* c, int count)
{
  for (int i=0; i<count;i++)
    {c[i]=0;}
  return 0;
}

void itoa(int n, char s[])
{
  int i, sign, specialcase=0;

       if ((sign = n) < 0)  /* record sign */
	    {
		 if (n != MIN_INT)
		   n = -n;          /* make n positive */
		 else  //special case
		   {
			n++;
			n = -n;
			specialcase=1;
		   } 
	    }
       i = 0;
       do {      /* generate digits in reverse order */
	    if ((i == 0) && specialcase )
		 {
		   s[i++] = n % 10 + '1';
		 }/* get next digit */
	    else
		 {
		   s[i++] = n % 10 + '0';  /* get next digit */
		 }
       }
	  while ((n /= 10) > 0);    /* delete it */
       if (sign < 0)
           s[i++] = '-';
       s[i] = '\0';
	  reverse(s);
}

void reverse(char s[])
{
  if (s!=0)
    {
	 int i=0;
	 while (s[i]!=0)
         i++;
	 char temp[i];
	 for (int j=0; j<i; j++)
	   {
		temp[j]=s[i-(j+1)];
	   }

	 for (int j=0; j<i; j++)
	   {
		s[j]=temp[j];
		s[j+1]=0;
	   }
	 
    }
}
